// 手机号中间4位用 **** 替换
const setPhoneHide = (val: string): string => {
    return val.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2")
}

//时间戳转时间
const getTimeStamp = (ns: number, sym = "-"): string => {
    ns = ns || new Date().getTime()
    const zero = function (m: number): string | number {
        return m < 10 ? "0" + m : m
    }
    const time = new Date(ns)
    const y = time.getFullYear()
    const m = time.getMonth() + 1
    const d = time.getDate()
    const h = time.getHours()
    const mm = time.getMinutes()
    const s = time.getSeconds()
    // return y + '-' + zero(m) + '-' + zero(d) + ' ' + zero(h) + ':' + zero(mm) + ':' + zero(s);
    return `${y}${sym}${zero(m)}${sym}${zero(d)}${sym}${zero(h)}${sym}${zero(mm)}${sym}${zero(s)}`
}

/**
 * 时间戳转小时
 * @param timestamp {{number}} 时间戳
 * @returns 
 */
const timestampToTime = (timestamp: number): string => {
    return (timestamp / 1000 / 60 / 60).toFixed(2) + "小时"
}


/**
 * 指定日期获取时间戳
 * @param date {{string}} 日期时间字符串
 * @returns {{number}} 日期时间戳
 */
const getTime = (date: string): number => {
    return new Date(date).getTime()
}


//格式化日期
const formatDateYear = (value: string, symbol = "-"): string => {
    const date = new Date(value)
    const y = date.getFullYear()
    let MM: string | number = date.getMonth() + 1
    MM = MM < 10 ? ("0" + MM) : MM
    let d: string | number = date.getDate()
    d = d < 10 ? ("0" + d) : d
    return y + symbol + MM + symbol + d
}


/**
 * 获取 url参数，返回对象
 * @param url   {{string}} 网址
 * @param name {{string}}  指定获取对象的key名称
 * @returns  {{object | string }}返回指定对象 | 对象属性
 */
const getQuerysObj = (url: string, name?: string): object | string => {
    url = url || window.location.search
    const obj: any = {}
    const reg = /[?&][^?&]+=[^?&]+/g
    const arr = url.match(reg)
    if (arr) {
        arr.forEach((item) => {
            const tempArr = item.substr(1).split("=")
            const key = decodeURIComponent(tempArr[0])
            const val = decodeURIComponent(tempArr[1])
            obj[key] = val
        })
    }
    if (name) return obj[name]
    return obj
}


/**
 * 睡眠函数
 * @param ms {{number}} 等待的时间，单位是ms
 * @returns void;
 */
const sleep = (ms = 600) => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(null)
        }, ms)
    })
}


/**
 * 文件下载{后端返回blob二级制流文件，前端直接下载}
 * @param data {{string | blob}} 二进制字符串或则blob流
 * @returns void;
 */
const fileDownload = (res: any): void => {
    //获取文件名
    let fileName = res.headers["content-disposition"]
    if (fileName?.length >= 2) fileName = fileName.split("=")[1]
    fileName = decodeURIComponent(fileName)
    const url = window.URL.createObjectURL((new Blob(res.data)))
    const link = document.createElement("a")
    link.href = url
    link.download = fileName
    link.click()
    window.URL.revokeObjectURL(url)
}


// todo ...

export {
    setPhoneHide,
    getTimeStamp,
    timestampToTime,
    getTime,
    formatDateYear,
    getQuerysObj,
    sleep,
    fileDownload,
}